package com.iotechn.unimall.admin.api.frontutil;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.iotechn.unimall.biz.service.config.ConfigBizService;
import com.iotechn.unimall.core.annotation.HttpMethod;
import com.iotechn.unimall.core.exception.ServiceException;
import com.iotechn.unimall.data.domain.*;
import com.iotechn.unimall.data.dto.ConfigDTO;
import com.iotechn.unimall.data.dto.ShopInfo;
import com.iotechn.unimall.data.dto.goods.LogDTO;
import com.iotechn.unimall.data.enums.SystemStatusType;
import com.iotechn.unimall.data.mapper.*;
import com.iotechn.unimall.data.model.Page;
import com.iotechn.unimall.data.util.SessionUtil;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class AdminFrontUtilServiceImpl implements AdminFrontUtilService {

    @Autowired
    private PlantGoodsCategoryMapper plantGoodsCategoryMapper;

    @Autowired
    private AreaMapper areaMapper;

    @Autowired
    private UserShopMapper userShopMapper;

    @Autowired
    private ShopOneMapper shopOneMapper;

    @Autowired
    private ShopDiscussMapper shopDiscussMapper;
    @Autowired
    private ConfigBizService configBizService;

    @Autowired
    private ShopLogMapper shopLogMapper;

    @Override
    public Object pgCateGory(Long adminId) throws Exception {
        List<PlantGoodsCategoryDO> plantGoodsCategoryDOS = plantGoodsCategoryMapper.selectList(new EntityWrapper<PlantGoodsCategoryDO>().eq("status", SystemStatusType.ENABLE.getCode()));
        return plantGoodsCategoryDOS;
    }

    @Override
    public Object areaOption(Long adminId) throws Exception {
        List<AreaDO> areaDOS = areaMapper.selectList(new EntityWrapper<AreaDO>().eq("status", SystemStatusType.ENABLE.getCode()));
        return areaDOS;
    }

    @Override
    public Object shopList() throws Exception {
        List<UserShopDO> areaDOS = userShopMapper.selectList(new EntityWrapper<UserShopDO>());
        return areaDOS;
    }
    //每个乡镇的项目情况 分组
    @Override
    public Object shopGroupByArea( ) throws Exception{
        Integer  status=null;
        List<ShopInfo> areaDOS = userShopMapper.selectListGroupByArea(status);
        return areaDOS;
    }

    @Override
    public Page<UserShopDO> shopListFront(Integer page, Integer limit) throws ServiceException {
        Wrapper<UserShopDO> wrapper = new EntityWrapper<UserShopDO>();
        wrapper.eq("status", 0);
        wrapper.orderBy("id", false);
        Integer count = userShopMapper.selectCount(wrapper);
        List<UserShopDO> UserShopDOS = userShopMapper.selectPage(new RowBounds((page - 1) * limit, limit), wrapper);
        return new Page<>(UserShopDOS, page, limit, count);
    }

    public List<ShopInfo> areaShopListFront() {
        List<ShopInfo> shopInfoList=userShopMapper.selectListGroupByArea(null);
        return shopInfoList;
    }


    @Override
    public Page<ShopDiscussDO> discussListFront(Integer page, Integer limit) throws ServiceException {
        Wrapper<ShopDiscussDO> wrapper = new EntityWrapper<ShopDiscussDO>();
         wrapper.orderBy("id", false);
        Integer count = shopDiscussMapper.selectCount(wrapper);
        List<ShopDiscussDO> UserShopDOS = shopDiscussMapper.selectPage(new RowBounds((page - 1) * limit, limit), wrapper);
        return new Page<>(UserShopDOS, page, limit, count);
    }

    @Override
    public Page<LogDTO> logListFront(Integer type,Integer page, Integer limit) throws ServiceException {
        List<LogDTO> LogDOS = shopLogMapper.getSpuDTOList(null, null, type,(page - 1) * limit, limit);
        Integer count = shopLogMapper.getSpuDTOListCount(null, null,type);
        Page<LogDTO> dtoPage = new Page<>(LogDOS, page, limit, count);
        return dtoPage;

    }
    @Override
    public Page<ShopOneDO> dakaListFront(Integer page, Integer limit) throws ServiceException {
        Wrapper<ShopOneDO> wrapper = new EntityWrapper<ShopOneDO>();
         wrapper.orderBy("id", false);
        Integer count = shopOneMapper.selectCount(wrapper);
        List<ShopOneDO> UserShopDOS = shopOneMapper.selectPage(new RowBounds((page - 1) * limit, limit), wrapper);
        return new Page<>(UserShopDOS, page, limit, count);
    }

    @Override
    public ConfigDTO getMerchant() {
        return configBizService.getMerchantConfig();
    }
}
